/*
Copyright (c) 2020-2022, Huawei Technologies Co., Ltd
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

* Description: 3D图表可视化插件数据绘制类
*/
#include "slt_space_draw.h"

namespace chart_plugin {
using std::string;
using std::vector;

const QString COLOR_LINE_PREFIX = "{x";
const QString COLOR_LINE_SUFFIX = "}-2";

const QString COLOR_FACE_PREFIX = "{x";
const QString COLOR_FACE_SUFFIX = "}";

const uint8_t OBJECT_DATA_NUM_SIZE = 8;
enum ObjectData {
    OBJECT_DATA_NUM0,
    OBJECT_DATA_NUM1,
    OBJECT_DATA_NUM2,
    OBJECT_DATA_NUM3,
    OBJECT_DATA_NUM4,
    OBJECT_DATA_NUM5,
    OBJECT_DATA_NUM6,
    OBJECT_DATA_NUM7,
};

SltSpaceDraw::SltSpaceDraw(SltSpaceDisplay *displayPtr, SltSpaceData *dataPtr)
    : spaceDisplayPtr(displayPtr), spaceDataPtr(dataPtr)
{}

void SltSpaceDraw::DrawOptimizedCurve(mglGraph *mglGraphPtr) const
{
    if (mglGraphPtr == nullptr) {
        return;
    }

    if (!(spaceDisplayPtr->GetOptimizedCurveLegend())->isChecked()) {
        return;
    }

    const vector<mglPoint> optimizedSltTrack = spaceDataPtr->GetOptimizedSltTrack();
    if (optimizedSltTrack.empty()) {
        return;
    }

    size_t optimizedDataSize = optimizedSltTrack.size();
    QColor colorCurve = (spaceDisplayPtr->GetOptimizedCurveColorProperty())->getColor();
    QString colorLineFormat = COLOR_LINE_PREFIX + colorCurve.name().mid(0x1) + COLOR_LINE_SUFFIX;
    size_t cycleTimes = optimizedDataSize - 1;
    for (size_t i = 0; i < cycleTimes; i++) {
        mglGraphPtr->Line(optimizedSltTrack[i], optimizedSltTrack[i + 1], colorLineFormat.toStdString().c_str());
    }
}

void SltSpaceDraw::DrawSpeedLimitTrackCurve(mglGraph *mglGraphPtr) const
{
    if (mglGraphPtr == nullptr) {
        return;
    }

    if (!(spaceDisplayPtr->GetSpeedLimitCurveLegend())->isChecked()) {
        return;
    }

    const vector<mglPoint> sltSpeedLimitTrack = spaceDataPtr->GetSltSpeedLimit();
    if (sltSpeedLimitTrack.empty()) {
        return;
    }

    size_t speedLimitDataSize = sltSpeedLimitTrack.size();
    QColor colorCurve = (spaceDisplayPtr->GetSpeedLimitCurveColorProperty())->getColor();
    QString colorLineFormat = COLOR_LINE_PREFIX + colorCurve.name().mid(0x1) + COLOR_LINE_SUFFIX;
    size_t cycleTimes = speedLimitDataSize - 1;
    for (size_t i = 0; i < cycleTimes; i++) {
        mglGraphPtr->Line(sltSpeedLimitTrack[i], sltSpeedLimitTrack[i + 1], colorLineFormat.toStdString().c_str());
    }
}

void SltSpaceDraw::DrawObjects(mglGraph *mglGraphPtr) const
{
    if (mglGraphPtr == nullptr) {
        return;
    }

    const auto sltObjectsMap = spaceDataPtr->GetObjectsMap();
    const auto objectsIdType = spaceDataPtr->GetObjectsIdTypeMap();
    const auto objectsTypeId = spaceDataPtr->GetObjectsTypeIdMap();

    for (auto iter = objectsTypeId.begin(); iter != objectsTypeId.end(); iter++) {
        int32_t objectType = iter.key();
        QStringList listId = iter.value();
        qSort(listId.begin(), listId.end());
        for (auto objectId : listId) {
            vector<mglPoint> objectData = sltObjectsMap.value(objectId);
            if (objectData.size() != OBJECT_DATA_NUM_SIZE) {
                continue;
            }
            spaceDisplayPtr->SetObjectProperty(objectType);
            if (!spaceDataPtr->GetObjectVisible(objectId)) {
                continue;
            }

            QColor color = (spaceDisplayPtr->GetObjectsTypeColorPropertyMap())[objectType]->getColor();
            QString colorFaceFormat = COLOR_FACE_PREFIX + color.name().mid(0x1) + COLOR_FACE_SUFFIX;
            mglGraphPtr->Face(objectData[OBJECT_DATA_NUM0], objectData[OBJECT_DATA_NUM1], objectData[OBJECT_DATA_NUM3],
            objectData[OBJECT_DATA_NUM2], colorFaceFormat.toStdString().c_str());
            mglGraphPtr->Face(objectData[OBJECT_DATA_NUM2], objectData[OBJECT_DATA_NUM3], objectData[OBJECT_DATA_NUM6],
            objectData[OBJECT_DATA_NUM7], colorFaceFormat.toStdString().c_str());
            mglGraphPtr->Face(objectData[OBJECT_DATA_NUM6], objectData[OBJECT_DATA_NUM7], objectData[OBJECT_DATA_NUM5],
            objectData[OBJECT_DATA_NUM4], colorFaceFormat.toStdString().c_str());
            mglGraphPtr->Face(objectData[OBJECT_DATA_NUM0], objectData[OBJECT_DATA_NUM1], objectData[OBJECT_DATA_NUM4],
            objectData[OBJECT_DATA_NUM5], colorFaceFormat.toStdString().c_str());
            mglGraphPtr->Face(objectData[OBJECT_DATA_NUM1], objectData[OBJECT_DATA_NUM2], objectData[OBJECT_DATA_NUM5],
            objectData[OBJECT_DATA_NUM6], colorFaceFormat.toStdString().c_str());
            mglGraphPtr->Face(objectData[OBJECT_DATA_NUM0], objectData[OBJECT_DATA_NUM3], objectData[	OBJECT_DATA_NUM4],
            objectData[OBJECT_DATA_NUM7], colorFaceFormat.toStdString().c_str());
        }
    }

    if (spaceDisplayPtr->GetIsTopicMessageComing()) {
        spaceDisplayPtr->SetIsTopicMessageComing(false);
        if (spaceDisplayPtr->IsObjectsSizeAndIdSame(objectsIdType)) {
            return;
        }
        spaceDisplayPtr->CreateObjectsLegend(objectsIdType);
        spaceDisplayPtr->UpdateLegend();
    } else {
        spaceDisplayPtr->UpdateLegend();
    }
}

int32_t SltSpaceDraw::Draw(mglGraph *mglGraphPtr)
{
    if (mglGraphPtr == nullptr) {
        return -1;
    }

    const double axisSMin = 0;
    const double axisSMax = 150;
    const double axisLMin = -5;
    const double axisLMax = 5;
    const double axisTMin = 0;
    const double axisTMax = 10;
    const double axisTickLength = 0.01;
    mglGraphPtr->Alpha(false);
    mglGraphPtr->Light(true);
    mglGraphPtr->SetRanges(axisSMin, axisSMax, axisLMin, axisLMax, axisTMin, axisTMax);
    mglGraphPtr->SetTickLen(axisTickLength);

    QString plotTitle = (spaceDisplayPtr->GetPlotTitleProperty())->getString();
    const double xRotateAxisAngles = 50;
    const double zRotateAxisAngles = 60;
    const string axisDirectionPara = "xyzUAKDTVISO";
    const string axisStyles = "{xF5F22E}-1";
    const string axisGridDirectionPara = "xyz";
    const string axisGridStyles = "{x007381}-1";

    const string titleStyles = "{xFFFFFF}";
    const double titleSize = -1.5;
    const char xAxis = 'x';
    const char yAxis = 'y';
    const char zAxis = 'z';
    const QString axisLabelPrefix = "#w{";
    const QString axisLabelSuffix = "}";
    const double axisLabelPos = 0.3;
    mglGraphPtr->Title(plotTitle.toStdString().c_str(), titleStyles.c_str(), titleSize);
    mglGraphPtr->Rotate(xRotateAxisAngles, zRotateAxisAngles);
    mglGraphPtr->Axis(axisDirectionPara.c_str(), axisStyles.c_str());
    mglGraphPtr->Grid(axisGridDirectionPara.c_str(), axisGridStyles.c_str());

    QString xAxisTitle = axisLabelPrefix + (spaceDisplayPtr->GetXAxisTitleProperty())->getString() + axisLabelSuffix;
    mglGraphPtr->Label(xAxis, xAxisTitle.toStdString().c_str(), axisLabelPos);
    QString yAxisTitle = axisLabelPrefix + (spaceDisplayPtr->GetYAxisTitleProperty())->getString() + axisLabelSuffix;
    mglGraphPtr->Label(yAxis, yAxisTitle.toStdString().c_str(), axisLabelPos);
    QString zAxisTitle = axisLabelPrefix + (spaceDisplayPtr->GetZAxisTitleProperty())->getString() + axisLabelSuffix;
    mglGraphPtr->Label(zAxis, zAxisTitle.toStdString().c_str(), axisLabelPos);

    DrawOptimizedCurve(mglGraphPtr);
    DrawSpeedLimitTrackCurve(mglGraphPtr);
    DrawObjects(mglGraphPtr);
    return 0;
}
} //  namespace chart_plugin
